package com.letv.core.parser;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.letv.core.bean.TipMapBean;
import com.letv.core.bean.TipMapBean.TipBean;
import com.letv.core.constant.LetvConstant;
import com.letv.core.db.PreferencesManager;

/**
 * 客户端提示语列表解析
 * 
 * @author zhuqiao
 *
 */
public class TipBeanListParser extends LetvMobileParser<TipMapBean> {
    private TipMapBean map;

    @Override
    protected TipMapBean parse(JSONObject data) throws Exception {
        if (isNull(data)) {
            return null;
        }

        JSONArray array = data.names();
        if (isNull(array)) {
            return null;
        }

        if (!isNewData()) {
            return null;
        }

        map = new TipMapBean();
        int length = array.length();
        map.setMax(length);

        for (int i = 0; i < length; i++) {
            parseBean(array.optString(i), data.optJSONObject(array.optString(i)));
        }

        PreferencesManager.getInstance().saveDialogMsgInfo("");

        return map;
    }

    private void parseBean(String name, JSONObject object) {
        if (TextUtils.isEmpty(name) || isNull(object)) {
            return;
        }

        TipBean bean = new TipBean();
        bean.setMsgId(name);
        bean.setMessage(object.optString("message"));
        bean.setTitle(object.optString("title"));
        map.getMap().put(name, bean);

        /**
         * 世界杯版本，新增口袋看球下载时间段和推送文案（v5.4.1）
         */
        if (TextUtils.equals(name, LetvConstant.DialogMsgConstantId.CONSTANT_70012)) {
            PreferencesManager.getInstance().setWorldCupTime(bean.getMessage());
        } else if (TextUtils.equals(name, LetvConstant.DialogMsgConstantId.CONSTANT_70014)) {
            PreferencesManager.getInstance().setWorldCupPushText(bean.getMessage());
        }
    }

}
